import { poiCatalog } from './poiCatalog'
import type { POIData } from '@/components/HospitalMapViewer'

// POI 类型枚举，方便使用
export const POI_TYPES = {
  DEPARTMENT: 'department',
  EQUIPMENT: 'equipment',
  ENTRANCE: 'entrance',
  ELEVATOR: 'elevator',
  STAIRS: 'stairs',
  TOILET: 'toilet',
  EMERGENCY: 'emergency'
} as const

// 快捷创建不同类型POI的工具函数
export class POIBuilder {
  private poi: Partial<POIData> = {}

  static create() {
    return new POIBuilder()
  }

  id(id: string) {
    this.poi.id = id
    return this
  }

  name(name: string) {
    this.poi.name = name
    return this
  }

  position(lng: number, lat: number) {
    this.poi.position = [lng, lat]
    return this
  }

  floor(floor: string) {
    this.poi.floor = floor
    return this
  }

  description(desc: string) {
    this.poi.description = desc
    return this
  }

  type(type: POIData['type']) {
    this.poi.type = type
    return this
  }

  metadata(data: Record<string, any>) {
    this.poi.metadata = data
    return this
  }

  // 快捷方法
  department(name: string, floor: string = 'L1') {
    this.poi.type = 'department'
    this.poi.name = name
    this.poi.floor = floor
    return this
  }

  equipment(name: string, floor: string = 'L1') {
    this.poi.type = 'equipment'
    this.poi.name = name
    this.poi.floor = floor
    return this
  }

  entrance(name: string) {
    this.poi.type = 'entrance'
    this.poi.name = name
    this.poi.floor = 'L1'
    return this
  }

  emergency(name: string, floor: string = 'L1') {
    this.poi.type = 'emergency'
    this.poi.name = name
    this.poi.floor = floor
    return this
  }

  build(): POIData {
    if (!this.poi.id || !this.poi.name || !this.poi.type || !this.poi.position || !this.poi.floor) {
      throw new Error('POI 缺少必需字段：id, name, type, position, floor')
    }
    return this.poi as POIData
  }

  // 构建并直接注册
  register() {
    const poi = this.build()
    poiCatalog.registerPOI(poi)
    return poi
  }
}

// 批量POI操作工具
export class POIBatch {
  private pois: POIData[] = []

  add(poi: POIData) {
    this.pois.push(poi)
    return this
  }

  addBuilder(builder: POIBuilder) {
    this.pois.push(builder.build())
    return this
  }

  // 为科室批量生成POI
  addDepartments(depts: Array<{
    id: string
    name: string
    position: [number, number]
    floor?: string
    description?: string
  }>) {
    depts.forEach(dept => {
      this.pois.push({
        id: dept.id,
        name: dept.name,
        type: 'department',
        position: dept.position,
        floor: dept.floor || 'L1',
        description: dept.description || ''
      })
    })
    return this
  }

  // 为设备批量生成POI
  addEquipment(equipment: Array<{
    id: string
    name: string
    position: [number, number]
    floor?: string
    description?: string
  }>) {
    equipment.forEach(equip => {
      this.pois.push({
        id: equip.id,
        name: equip.name,
        type: 'equipment',
        position: equip.position,
        floor: equip.floor || 'L1',
        description: equip.description || ''
      })
    })
    return this
  }

  build() {
    return this.pois
  }

  register() {
    poiCatalog.registerPOIs(this.pois)
    return this.pois
  }

  clear() {
    this.pois = []
    return this
  }
}

// 便捷的创建函数
export function createPOI() {
  return POIBuilder.create()
}

export function createBatch() {
  return new POIBatch()
}

// 从坐标计算相对位置的工具函数
export function calculateNearbyPosition(
  basePosition: [number, number],
  offsetMeters: number,
  direction: 'north' | 'south' | 'east' | 'west' | 'northeast' | 'northwest' | 'southeast' | 'southwest'
): [number, number] {
  const [baseLng, baseLat] = basePosition
  const meterToLng = offsetMeters / 111320 // 大约1度经度 = 111320米（赤道）
  const meterToLat = offsetMeters / 110540 // 大约1度纬度 = 110540米

  const directionMap = {
    north: [0, meterToLat],
    south: [0, -meterToLat],
    east: [meterToLng, 0],
    west: [-meterToLng, 0],
    northeast: [meterToLng * 0.7, meterToLat * 0.7],
    northwest: [-meterToLng * 0.7, meterToLat * 0.7],
    southeast: [meterToLng * 0.7, -meterToLat * 0.7],
    southwest: [-meterToLng * 0.7, -meterToLat * 0.7]
  }

  const [offsetLng, offsetLat] = directionMap[direction]
  return [baseLng + offsetLng, baseLat + offsetLat]
}

export default {
  POIBuilder,
  POIBatch,
  createPOI,
  createBatch,
  calculateNearbyPosition,
  POI_TYPES
}